package ljl.codetop300;

public class _146_spiral_print {

  static class test {

    public int[] spiralArray(int[][] array) {
      int m = array.length;
      if (m == 0) return new int[0];
      int n = array[0].length;
      if (n == 0) return new int[0];
      int t = 0, b = m - 1, l = 0, r = n - 1;
      int[] res = new int[m * n];
      int k = 0;
      while (l <= r && t <= b) {
        for (int i = l; i <= r; i++) {
          res[k++] = array[t][i];
        }
        for (int i = t + 1; i <= b; i++) {
          res[k++] = array[i][r];
        }
        if (b > t) {
          for (int i = r - 1; i >= l ; i--) {
            res[k++] = array[b][i];
          }
        }
        if (l < r) {
          for (int i = b - 1; i > t; i--) {
            res[k++] = array[i][l];
          }
        }
        l++;
        r--;
        t++;
        b--;
      }
      return res;
    }
  }
}
